package th.co.exat.uap.model;

import com.channgam.dbmlib.sql.MySQLJDBC;
import com.channgam.dbmlib.sql.iDataAdapter;
import java.util.ArrayList;
import java.util.List;
import th.co.exat.uap.config.Config;
import th.co.exat.uap.util.date.FormateDate;

public class EditLog {

    private static iDataAdapter i;
    private String id;
    private String program;
    private String uid;
    private String editDate;
    private String editBy;

    public static List<EditLog> getDataList(String program,String uid) throws Exception {
        List<EditLog> l = new ArrayList<EditLog>();

        i = new MySQLJDBC(Config.getMySqlHost(),
                Config.getMySqlUsername(),
                Config.getMySqlPassword(),
                Config.getMySqlDbname());
        i.connect();


        String sql = "SELECT * "
                + "FROM uap_trx_uap_editlog "
                + "WHERE 1 "
                + "AND program = '" + program + "' " +
                "AND uid = '" + uid + "' " +
                "ORDER BY id ";
        
        i.query(sql);

        while (i.resultNext()) {
            EditLog e = new EditLog();
            e.setId(i.result().getString("id"));
            e.setProgram(i.result().getString("program"));
            e.setUid(i.result().getString("uid"));
            e.setEditDate(FormateDate.convertDateTimeToWebapp(i.result().getString("edit_date")));
            e.setEditBy(i.result().getString("edit_by"));
            l.add(e);
        }

        i.close();
        return l;
    }

    public static int insertData(EditLog e) throws Exception {
        int ex = 0;
        i = new MySQLJDBC(Config.getMySqlHost(),
                Config.getMySqlUsername(),
                Config.getMySqlPassword(),
                Config.getMySqlDbname());
        i.connect();

        String sql = "INSERT INTO uap_trx_uap_editlog "
                + "(program, uid, edit_by) "
                + "VALUES "
                + "('" + e.getProgram() + "',"
                + "'" + e.getUid() + "',"
                + "'" + e.getEditBy() + "')";

        i.statement(sql);
        ex = i.execute();
        i.close();
        return ex;
    }

    public String getEditBy() {
        return editBy;
    }

    public void setEditBy(String editBy) {
        this.editBy = editBy;
    }

    public String getEditDate() {
        return editDate;
    }

    public void setEditDate(String editDate) {
        this.editDate = editDate;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getProgram() {
        return program;
    }

    public void setProgram(String program) {
        this.program = program;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }
}
